namespace SimpleWebServices.Web.Routing
{
    using System;
    using System.Web;
    using System.Web.Routing;
    using Moq;
    using SimpleWebServices;
    using SimpleWebServices.Configuration;
    using SimpleWebServices.Data;
    using SimpleWebServices.Web.Mvc;
    using Xunit;

    public sealed class ResourceRouteFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<ResourceRoute<IResourceDummy>>()
                .DerivesFrom<RouteBase>()
                .IsConcreteClass()
                .IsSealed()
                .NoDefaultConstructor()
                .Result);
        }

        [Fact]
        public void ctor_Type()
        {
            Assert.NotNull(new ResourceRoute<IResourceDummy>(typeof(ResourceControllerOfIResourceDummy)));
        }

        [Fact]
        public void ctor_TypeInvalid()
        {
            Assert.Throws<ArgumentOutOfRangeException>(() => new ResourceRoute<IResourceDummy>(typeof(string)));
        }

        [Fact]
        public void ctor_TypeNull()
        {
            Assert.Throws<ArgumentNullException>(() => new ResourceRoute<IResourceDummy>(null));
        }

        [Fact]
        public void prop_Controller_get()
        {
            Type expected = typeof(ResourceControllerOfIResourceDummy);
            Type actual = new ResourceRoute<IResourceDummy>(expected).Controller;

            Assert.Same(expected, actual);
        }

        [Fact]
        public void prop_Controller_setInvalid()
        {
            var obj = new ResourceRoute<IResourceDummy>(typeof(ResourceControllerOfIResourceDummy));

            Assert.Throws<ArgumentOutOfRangeException>(() => obj.Controller = typeof(string));
        }

        [Fact]
        public void prop_Controller_setNull()
        {
            var obj = new ResourceRoute<IResourceDummy>(typeof(ResourceControllerOfIResourceDummy));

            Assert.Throws<ArgumentNullException>(() => obj.Controller = null);
        }

        [Fact]
        public void op_GetRouteData_HttpContextBaseNull()
        {
            Assert.Throws<ArgumentNullException>(() => new ResourceRoute<IResourceDummy>(typeof(ResourceControllerOfIResourceDummy)).GetRouteData(null));
        }

        [Fact]
        public void op_GetRouteData_HttpContextBase_lanneg()
        {
            FakeRepository.Clear();

            try
            {
                FakeRepository.Register<TestResource>(new
                {
                    urn = "urn://example.com/concrete/test",
                    xml = "<concrete />"
                });

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.AppRelativeCurrentExecutionFilePath).Returns("~/concrete/test").Verifiable();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/concrete/test").Verifiable();
                mock.SetupGet<string>(x => x.Request.PathInfo).Returns(string.Empty).Verifiable();
                HttpContextFactory.Mock = mock.Object;

                RouteData actual = new ResourceRoute<TestResource>(typeof(TestResourceController)).GetRouteData(mock.Object);

                Assert.Equal<string>("concrete/test", (actual.Route as Route).Url);
                Assert.Equal<string>("TestResource", (string)actual.Values["controller"]);
                Assert.Equal<string>("LanguageNegotiation", (string)actual.Values["action"]);
                Assert.Equal<string>(string.Empty, (string)actual.Values["language"]);

                mock.VerifyAll();
            }
            finally
            {
                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void op_GetRouteData_HttpContextBase_conneg()
        {
            FakeRepository.Clear();

            try
            {
                FakeRepository.Register<TestResource>(new
                {
                    urn = "urn://example.com/concrete/test",
                    xml = "<concrete />"
                });

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.AppRelativeCurrentExecutionFilePath).Returns("~/concrete/test.en").Verifiable();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/concrete/test.en").Verifiable();
                mock.SetupGet<string>(x => x.Request.PathInfo).Returns(string.Empty).Verifiable();
                HttpContextFactory.Mock = mock.Object;

                RouteData actual = new ResourceRoute<TestResource>(typeof(TestResourceController)).GetRouteData(mock.Object);

                Assert.Equal<string>("concrete/test.en", (actual.Route as Route).Url);
                Assert.Equal<string>("TestResource", (string)actual.Values["controller"]);
                Assert.Equal<string>("ContentNegotiation", (string)actual.Values["action"]);
                Assert.Equal<string>("en", (string)actual.Values["language"]);

                mock.VerifyAll();
            }
            finally
            {
                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void op_GetRouteData_HttpContextBase_html()
        {
            FakeRepository.Clear();

            try
            {
                FakeRepository.Register<TestResource>(new
                {
                    urn = "urn://example.com/concrete/test",
                    xml = "<concrete />"
                });

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.AppRelativeCurrentExecutionFilePath).Returns("~/concrete/test.en.html").Verifiable();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/concrete/test.en.html").Verifiable();
                mock.SetupGet<string>(x => x.Request.PathInfo).Returns(string.Empty).Verifiable();
                HttpContextFactory.Mock = mock.Object;

                RouteData actual = new ResourceRoute<TestResource>(typeof(TestResourceController)).GetRouteData(mock.Object);

                Assert.Equal<string>("concrete/test.en.html", (actual.Route as Route).Url);
                Assert.Equal<string>("TestResource", (string)actual.Values["controller"]);
                Assert.Equal<string>("HtmlRepresentation", (string)actual.Values["action"]);
                Assert.Equal<string>("en", (string)actual.Values["language"]);

                mock.VerifyAll();
            }
            finally
            {
                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void op_GetRouteData_HttpContextBase_null()
        {
            FakeRepository.Clear();

            try
            {
                FakeRepository.Register<TestResource>(new
                {
                    urn = "urn://example.com/concrete/test",
                    xml = "<concrete />"
                });

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/not-a-matching-path").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                Assert.Null(new ResourceRoute<TestResource>(typeof(TestResourceController)).GetRouteData(mock.Object));

                mock.VerifyAll();
            }
            finally
            {
                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void op_GetVirtualPath_RequestContextNull_RouteValueDictionary()
        {
            Assert.Throws<ArgumentNullException>(() => new ResourceRoute<IResourceDummy>(typeof(ResourceControllerOfIResourceDummy)).GetVirtualPath(null, new RouteValueDictionary()));
        }

        [Fact]
        public void op_GetVirtualPath_RequestContext_RouteValueDictionary()
        {
            FakeRepository.Clear();

            try
            {
                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/sample/test").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                FakeRepository.Register<IResourceDummy>(new
                {
                    urn = "urn://example.com/sample/test",
                    xml = "<test />"
                });

                RequestContext request = new RequestContext(mock.Object, new RouteData());

                Assert.NotNull(new ResourceRoute<IResourceDummy>(typeof(RouteCollectionTestController)).GetVirtualPath(request, new RouteValueDictionary()));

                mock.VerifyAll();
            }
            finally
            {
                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }
    }
}